package com.biboheart.breport.core;

import com.biboheart.breport.dataset.BrReport;
import com.biboheart.breport.engine.BrConvertor;
import com.biboheart.breport.engine.BrConvertorManager;
import com.biboheart.breport.engine.convert.BrConvertorManagerImpl;
import com.biboheart.breport.engine.engine.BrEngineSample;
import com.biboheart.breport.source.BrDataSource;
import com.biboheart.breport.template.BrTemplate;

public class BrHandler {
    private final BrContext context;
    private final BrEngineSample engine;
    private final BrConvertorManager convertorManager;

    public BrHandler(BrDataSource dataSource, BrTemplate template) {
        convertorManager = new BrConvertorManagerImpl();
        context = BrContext.builder()
                .dataSource(dataSource)
                .template(template)
                .convertorManager(convertorManager)
                .build();
        engine = new BrEngineSample(context);
    }

    public void register(String sn, BrConvertor convert) {
        convertorManager.register(sn, convert);
    }

    public BrReport run() {
        engine.setDataSource(context.getDataSource());
        engine.setTemplate(context.getTemplate());
        return engine.generate();
    }
}
